#ifndef __H264DECODER_H__
#define __H264DECODER_H__

#include "MyThread.h"
#include "ListPtr.h"
#include "Lock.h"
#include "define.h"

extern "C"
{
#include "libavutil/avutil.h"
#include "libavcodec/avcodec.h"
#include "libswscale/swscale.h"
};

typedef struct tagAvFrameInfo
{
	int			nChannelId;
	AVFrame		*pAvFrame;
	tagAvFrameInfo()
	{
		pAvFrame = NULL;
	}
}AvFrameInfo;

class DecodeDataNotify
{
public:
	DecodeDataNotify(void){};
	virtual~DecodeDataNotify(void){};
public:
	virtual void OnDecodeData(int nId, int nFrameRate, AVFrame *pAvFrame)=0;
};

class CH264Decoder : CMyThread
{
public:
	CH264Decoder(int nIndex, DecodeDataNotify *pNotify);
	~CH264Decoder(void);

	void PutRecvData(RecvDataInfo *pRecvData);
	void SetVideoParam(int nWidth, int nHeigth);

protected:
	virtual void ThreadProcMain(void);

private:
	bool InitDecoder(void);
	int DecoderData(char *pData, int nDataLen);

	void DelRecvData(RecvDataInfo *pRecvData);

private:
	bool					m_bExit;
	int						m_nIndex;

	struct AVCodec			*m_pCode;
	struct AVCodecContext	*m_pAvc;

	struct AVFrame			*m_pFrame;
	struct AVFrame			*m_pYUVFrame;	

	ListPtr					m_lstAVFrame;
	CLock					m_lockAvFrame;

	ListPtr					m_lstRecvData;
	CLock					m_lockRecvData;

	struct AVPacket			m_avPkt;
	DecodeDataNotify		*m_pNotify;

	int						m_nSrcWidth;
	int						m_nSrcHeight;

	int						m_nDstWidth;
	int						m_nDstHeigth;

	uint8_t					*m_pOutBuffer;
	struct SwsContext		*m_pImg_convert_ctx;

	FILE					*m_pOutFile;
	int						m_nFrameRate;
};

#endif
